package com.zeny.edu.video.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.zeny.edu.common.EduException;
import com.zeny.edu.video.service.VidService;
import com.zeny.edu.video.util.AliyunVODSDKUtils;
import com.zeny.edu.video.util.ConstantPropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @ClassName VidServiceImpl
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/6 0006 23:57
 */
@Service
public class VidServiceImpl implements VidService {
    /**
     * @param file
     * @return java.lang.String
     * @Description 视频上传到阿里云
     * @Date 2020/3/7 0007 0:00
     **/
    @Override
    public String uploadVideoAliyun(MultipartFile file) {
        String videoId = "";
        try {
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            //请求视频点播服务的请求ID
            System.out.print("RequestId=" + response.getRequestId() + "\n");
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
            } else {
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            videoId = response.getVideoId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return videoId;
    }

    /**
     * @param videoId
     * @return void
     * @Description 根据videoID删除视频
     * @Date 2020/3/7 0007 18:31
     **/
    @Override
    public void removeVideo(String videoId) {
        try {
            DefaultAcsClient client = AliyunVODSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            throw new EduException(20002, "视频删除失败");
        }
    }

    /**
     * @Description 删除多个视频
     * @Date 2020/3/8 0008 8:06
     * @param videoList
     * @return void
     **/
    @Override
    public void deleteMoreVideo(List<String> videoList) {
        try {
            DefaultAcsClient client = AliyunVODSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            String videoIds = StringUtils.join(videoList, ",");
            request.setVideoIds(videoIds);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            throw new EduException(20002, "视频删除失败");
        }
    }
}
